package com.dong.config;

import com.dong.service.PersonService;
import com.dong.service.PersonServiceImpl;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator;
import org.springframework.context.annotation.AutoProxyRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration;
import org.springframework.transaction.annotation.TransactionManagementConfigurationSelector;
import org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/***
 * 通过  {@link TransactionManagementConfigurationSelector} 向容器注册bean
 *         {@link AutoProxyRegistrar}
 *                  {@link InfrastructureAdvisorAutoProxyCreator}
 *         {@link ProxyTransactionManagementConfiguration}
 *              引入 {@link BeanFactoryTransactionAttributeSourceAdvisor}
 *                  {@link TransactionAttributeSource}
 *                  {@link TransactionInterceptor}
 * AutoProxyRegistrar:注册Bean
 *
 *          InfrastructureAdvisorAutoProxyCreator: 增强对象,使用方法拦截器进行增强
 *
 * ProxyTransactionManagementConfiguration:  给容器中注册事务增强器  ,RollbackFor....
 *     1. 给容器总注册事务层增强器?
 *          1.1 时区增强器要用事务注解信息,AnnotationTransactionAttributeSource 解析事务
 *          1.2 拦截事务 {@link  TransactionAspectSupport}
 *                  {@link TransactionInterceptor}: 保存了事务属性信息[transactionManager],事务管理器
 *                  它是一个方法拦截器
 *                  在目标方法执行的时候,进行方法拦截
 *                          1. 获取事务相关属性
 *                          2. 在获取PlatformTransactionManager,如果没有指定,则从容器直接获取
 *                                  正常: 获取事务管理器  提交
 *                                  异常: 回滚
 *
 *
 *
 */
@Configuration
@EnableTransactionManagement
public class T120TxConfig {
    @Bean
    public DataSource dataSource () throws PropertyVetoException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setUser("root");
        ds.setPassword("root");
        ds.setJdbcUrl("jdbc:mysql://localhost:3306/spring_learn_test?useSSL=false");
        ds.setDriverClass("com.mysql.jdbc.Driver");
        return ds;

    }
    @Bean
    public JdbcTemplate jdbcTemplate () throws PropertyVetoException {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return jdbcTemplate;
    }
    @Bean
    public PersonService personService() {
        return new PersonServiceImpl();
    }
    @Bean
    public DataSourceTransactionManager transactionManager() throws PropertyVetoException {
        return new DataSourceTransactionManager(dataSource());
    }
}
